const WXAPI = require('apifm-wxapi')
const TOOLS = require('../../utils/tools.js')
const AUTH = require('../../utils/auth')
const CONFIG = require('../../config.js')
const APP = getApp()

Page({
  data: {
    qj: wx.getStorageSync("qj") ? wx.getStorageSync("qj") : 'bz',
    zdydh: wx.getStorageSync("zdydh") ? wx.getStorageSync("zdydh") : '',
    zdylogo: wx.getStorageSync("zdylogo") ? wx.getStorageSync("zdylogo") : '',
    dh_tap_index: 0,
    jdy: {},
    pageTheme: '',
    kjl3d: wx.getStorageSync("kjl3d") ? wx.getStorageSync("kjl3d") : '',
    sphid: wx.getStorageSync("sphid") ? wx.getStorageSync("sphid") : '',
  },
  onLoad: function (e) {


    this.cmsPage('yt0002');

  },





  goSearch() {
    wx.navigateTo({
      url: "/pages/search/index",
    });
  },
  goNotice(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/notice/show?id=" + id,
    });
  },
  go(e) {

    if (e.currentTarget.dataset.id == '视频号' && this.data.sphid) {
      wx.openChannelsUserProfile({
        finderUserName: this.data.sphid,
        // activityId: 'profile_' + this.data.sphid, // 格式固定：profile_ + 主页ID
        success: (res) => console.log('跳转成功', res),
        fail: (err) => {
          console.log('跳转失败', err);
          wx.showToast({ title: '跳转失败', icon: 'none' });
        }
      });
    }
    else if (e.currentTarget.dataset.id == '酷家乐3D' && this.data.kjl3d) {
      wx.navigateTo({
        url: `/packageYt/webview/index?url=${this.data.kjl3d}`
      });
    } else {
      wx.navigateTo({
        url: `/packageYt/index/list?id=${e.currentTarget.dataset.id}`
      });
    }


  },

  async cmsPage(e) {
    const res = await WXAPI.cmsPage(e)

    let info = res.data.info.content;
    info = info.replace(/\<img/g, "<img style='max-width: 100%;width:auto;height:auto;margin:10px 0;float:left;display:block '")

    info = info.replace(/\<h3/g, "<h3 style='margin:20px 0;'")
    info = info.replace(/\<h2/g, "<h2 style='margin:20px 0;'")
    info = info.replace(/\<h4/g, "<h4 style='margin:20px 0;'")
    info = info.replace(/\<ul/g, "<ul style='margin:20px 0;'")
    info = info.replace(/\<ol/g, "<ol style='margin:20px 0;'")


    console.log("ccc", info);

    let temp = res.data.extJson;
    if (temp.LC1_1) {
      temp.LC1_1 = temp.LC1_1.split("、");
    }
    if (temp.LC1_1a) {
      temp.LC1_1a = temp.LC1_1a.split("、");
    }
    if (temp.LC2_2) {
      temp.LC2_2 = temp.LC2_2.split("、");
    }
    if (temp.LC2_2a) {
      temp.LC2_2a = temp.LC2_2a.split("、");
    }
    if (temp.LC3_3) {
      temp.LC3_3 = temp.LC3_3.split("、");
    }
    if (temp.LC3_3a) {
      temp.LC3_3a = temp.LC3_3a.split("、");
    }
    if (temp.LC4_4) {
      temp.LC4_4 = temp.LC4_4.split("、");
    }
    if (temp.LC4_4a) {
      temp.LC4_4a = temp.LC4_4a.split("、");
    }
     if (temp.LC4_4F) {
      temp.LC4_4F = temp.LC4_4F.split("、");
    }
    if (temp.LC4_4aF) {
      temp.LC4_4aF = temp.LC4_4aF.split("、");
    }
    if (temp.LC5_5) {
      temp.LC5_5 = temp.LC5_5.split("、");
    }
    if (temp.LC5_5a) {
      temp.LC5_5a = temp.LC5_5a.split("、");
    }
    if (temp.LC6_6) {
      temp.LC6_6 = temp.LC6_6.split("、");
    }
    if (temp.LC6_6a) {
      temp.LC6_6a = temp.LC6_6a.split("、");
    }
    if (temp.LC7_7) {
      temp.LC7_7 = temp.LC7_7.split("、");
    }
    if (temp.LC7_7a) {
      temp.LC7_7a = temp.LC7_7a.split("、");
    }
    if (temp.LC8_8) {
      temp.LC8_8 = temp.LC8_8.split("、");
    }
    if (temp.LC8_8a) {
      temp.LC8_8a = temp.LC8_8a.split("、");
    }
    if (temp.LC9_9) {
      temp.LC9_9 = temp.LC9_9.split("、");
    }
    if (temp.LC9_9a) {
      temp.LC9_9a = temp.LC9_9a.split("、");
    }
    if (temp.LC10_10) {
      temp.LC10_10 = temp.LC10_10.split("、");
    }
    if (temp.LC10_10a) {
      temp.LC10_10a = temp.LC10_10a.split("、");
    }

    console.log(temp);



    this.setData({
      jdy: temp,
      // info: info
    })


  },

  zdy_top_nav() {
    wx.navigateTo({
      url: '/pages/xl/index',
    })
  },
  dh_tap(e) {
    //console.log(e);
    let temp = e.currentTarget.dataset.id;
    this.setData({
      dh_tap_index: temp
    })
    if (temp == 0) {
      wx.reLaunch({
        url: '/packageYt/index/index',
      })
    }
    if (temp == 1) {
      wx.reLaunch({
        url: '/packageYt/case/index',
      })
    }
    if (temp == 2) {
      wx.reLaunch({
        url: '/packageYt/ting/index',
      })
    }
    if (temp == 3) {
      wx.reLaunch({
        url: '/packageYt/goods/index',
      })
    }
    if (temp == 4) {
      wx.reLaunch({
        url: '/packageYt/my/index',
      })
    }
  },


  onShow: function (e) {
    this.setData({
      qj: wx.getStorageSync("qj") ? wx.getStorageSync("qj") : 'bz'
    })

  },

  async getGoodsList(categoryId, append) {
    if (categoryId == 0) {
      categoryId = "";
    }
    wx.showLoading({
      title: ''
    })
    // https://www.yuque.com/apifm/nu0f75/wg5t98
    const res = await WXAPI.goodsv2({
      categoryId: categoryId,
      page: this.data.curPage,
      pageSize: this.data.pageSize
    })
    wx.hideLoading()
    if (res.code == 404 || res.code == 700) {
      let newData = {
        loadingMoreHidden: false
      }
      if (!append) {
        newData.goods = []
      }
      this.setData(newData);
      return
    }
    let goods = [];
    if (append) {
      goods = this.data.goods
    }
    for (var i = 0; i < res.data.result.length; i++) {
      const item = res.data.result[i]
      const hidden_goods_index = wx.getStorageSync('hidden_goods_index')
      if (hidden_goods_index.indexOf(item.id) != -1) {
        continue
      }
      goods.push(item);
    }
    this.setData({
      loadingMoreHidden: true,
      goods: goods,
    });
  },

  onShareAppMessage: function () {
    return {
      title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
      // path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
    }
  },
  onShareTimeline() {
    return {
      title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
      // query: 'inviter_id=' + wx.getStorageSync('uid'),
      // imageUrl: wx.getStorageSync('share_pic')
    }
  },

  onReachBottom: function () {
    // this.setData({
    //   curPage: this.data.curPage + 1
    // });
    // this.getGoodsList(0, true)
  },
  onPullDownRefresh: function () {
    // this.setData({
    //   curPage: 1
    // });
    // this.getGoodsList(0)
    wx.stopPullDownRefresh()
  }
})